package com.example.flowableoa.service.Impl;




import com.example.flowableoa.common.ProcessConstant;
import com.example.flowableoa.domain.dto.FlowDeployProcDto;
import com.example.flowableoa.factory.FlowServiceFactory;
import com.example.flowableoa.service.IflowDefinitionService;
import org.flowable.engine.history.HistoricProcessInstance;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.repository.ProcessDefinition;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @Author:李博文
 * @Date:2022/10/31 14:43
 */
@Service
public class IflowDefinitionServiceImpl extends FlowServiceFactory implements IflowDefinitionService {




    //部署流程
    @Override
    public void deployFlow(String name, String categroy, InputStream inputStream) {
        repositoryService.createDeployment()
                .addInputStream(name+ ProcessConstant.BPMN_FILE_SUFFIX,inputStream)
                .name(name)
                .category(categroy)
                .deploy();

    }

    /*
    * 查询流程
     */

    @Override
    public List<FlowDeployProcDto> list() {

//
//        List<HistoricProcessInstance> his = historyService.createHistoricProcessInstanceQuery().startedBy("发起人").list();
//        for (HistoricProcessInstance hi : his) {
//            hi.ge
//        }

        List<FlowDeployProcDto> list  = new ArrayList<>();
        List<ProcessDefinition> ProcessDefinitions = repositoryService.createProcessDefinitionQuery().latestVersion().list();

        System.err.println(ProcessDefinitions);

        if(ProcessDefinitions!=null && ProcessDefinitions.size()>0){
            for (ProcessDefinition processDefinition : ProcessDefinitions) {
                FlowDeployProcDto dto = new FlowDeployProcDto();
                // 根据部署的流程ID查找对应的 流程定义信息
                Deployment deployment = repositoryService
                        .createDeploymentQuery()
                        .deploymentId(processDefinition.getDeploymentId())
                        .singleResult();
                dto.setId(processDefinition.getId());
                // deployment.getName()  流程部署的名称
                // processDefinition.getName() 流程定义的名称
                dto.setName(processDefinition.getName());
                dto.setCategory(processDefinition.getCategory());
                dto.setTenantId(processDefinition.getTenantId());
                dto.setDeployId(processDefinition.getDeploymentId());
                dto.setFlowKey(processDefinition.getKey());
                dto.setDeployDate(deployment.getDeploymentTime());

                dto.setDescription(processDefinition.getDescription());
                dto.setVersion(processDefinition.getVersion());
                dto.setSuspensionState(processDefinition.isSuspended()?2:1);
                list.add(dto);
            }
        }
        return list;
    }

    @Override
    public List<FlowDeployProcDto> list2(String guanjianzi) {
        List<FlowDeployProcDto> list  = new ArrayList<>();
        List<ProcessDefinition> ProcessDefinitions = repositoryService.createProcessDefinitionQuery().processDefinitionNameLike("%"+guanjianzi+"%").latestVersion().list();

        System.err.println(ProcessDefinitions);

        if(ProcessDefinitions!=null && ProcessDefinitions.size()>0){
            for (ProcessDefinition processDefinition : ProcessDefinitions) {
                FlowDeployProcDto dto = new FlowDeployProcDto();
                // 根据部署的流程ID查找对应的 流程定义信息
                Deployment deployment = repositoryService
                        .createDeploymentQuery()
                        .deploymentId(processDefinition.getDeploymentId())
                        .singleResult();
                dto.setId(processDefinition.getId());
                // deployment.getName()  流程部署的名称
                // processDefinition.getName() 流程定义的名称
                dto.setName(processDefinition.getName());
                dto.setCategory(processDefinition.getCategory());
                dto.setTenantId(processDefinition.getTenantId());
                dto.setDeployId(processDefinition.getDeploymentId());
                dto.setFlowKey(processDefinition.getKey());
                dto.setDeployDate(deployment.getDeploymentTime());
                dto.setDescription(processDefinition.getDescription());
                dto.setVersion(processDefinition.getVersion());
                dto.setSuspensionState(processDefinition.isSuspended()?2:1);
                list.add(dto);
            }
        }
        return list;



    }


    //更新流程的状态
    @Override
    public void updatestate(String definitionId) {
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .processDefinitionId(definitionId)
                .singleResult();
        if(processDefinition.isSuspended()){
            //这里表示是挂起状态 要激活
            repositoryService.activateProcessDefinitionById(definitionId,true,null);
        }else{
            //这里是激活状态 ，要挂起
            repositoryService.suspendProcessDefinitionById(definitionId,false,null);

        }
    }

    @Override
    public void deleteFlow(String deployId) {
//        System.err.println("???进来了吗");
        // 第二个true表示这个流程 对应的实例，级联，任务，历史数据都会被删除
        repositoryService.deleteDeployment(deployId,true);
    }

    @Override
    public byte[]  flowImg(String deployId) {
        System.err.println(deployId);
        //查询到指定的文件
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deployId).singleResult();
        //这个方法是获取图片的
        String resourceName = processDefinition.getDiagramResourceName();
        System.err.println(resourceName);
        if(resourceName ==null){
            return null;
        }
        InputStream resourceAsStream = repositoryService.getResourceAsStream(deployId, resourceName);
        try {
            //resourceAsStream.available 是获取该流的长度
            byte [] img = new byte[resourceAsStream.available()];
            //read 方法是把resource 里面的数据给 img
            resourceAsStream.read(img);
            return img;


        } catch (IOException e) {
            e.printStackTrace();
        }
        return null ;

    }
}
